#include "userDao.h"

void saveUser(User* user)
{
    sqlite3 *db = NULL;
    int res = sqlite3_open("/home/fcl/ronghuanet_ads/test.db",&db);
    if (res != 0)
    {
        perror("open data db error");
    }
    char *sql = (char *)calloc(1,256);
    sprintf(sql,"insert into t_user(id,name) values(%d,'%s')",user->id,user->name);
    char *errmsg;

    sqlite3_exec(db,sql,NULL,NULL,&errmsg);
    if (errmsg != NULL)
    {
        printf("%s\n",errmsg);
    }
    sqlite3_close(db);
}


void getUserAll(){
    sqlite3 *db = NULL;
    int res = sqlite3_open("/home/fcl/ronghuanet_ads/test.db",&db);
    if(res != 0)
    {
        perror("open data db error");
        return;
    }
    char *sql = "select * from t_user";
    char *errmsg;
    int row;
    int col;
    char** result;
    sqlite3_get_table(db,sql,&result,&row,&col,&errmsg);
    printf("row total count:%d\n",row);
    printf("col total count:%d\n",col);

    int index = col;

    printf("查到%d条记录\n",row);

    for(int i = 0; i < row; i++)
    {
        printf("查到%d条记录\n",i+1);
        for(int j = 0; j < col; j++)
        {
            printf("字段名：%s > 字段值:%s\n",result[j],result[index]);
            ++index;
        }
    }
     /*
     int i, j;
    for(i = 1; i <= row; i++) {
        for(j = 0; j < col; j++) {
            int temp=i * col + j; //0 1 2 3 4 5 6 7 8
            //printf("%s ---%d---%d---%d", result[temp],i,j,temp);
            printf("%s ---%d", result[temp],temp);
        }
        printf("\n");
    } 
    */

}